package xfuzzy.alg_genetico.indeterminado.output;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import xfuzzy.alg_genetico.indeterminado.view.ViewDefinitions;

public class ConfigFile implements ConfigProperties  {
	
	private double mutationProbability;
	private long mutationSeed;
	private double crossProbability;
	private String crossSelected;
	private long crossSeed;
	private int populationSize;
	private long populationSeed;
	private long reparationSeed;
	private String stopConditionSelected;
	private int numberEvaluations;
	private int generationsWithoutImprovement;
	private double optimusValue;
	private int numberGenerations;
	private int minutesTimeLimit;
	private int hoursTimeLimit;
	private String selectionSelected;
	private int tournamentSizeSelection;
	private long selectionSeed;
	private String evaluationSelected; 
	private String replacementSelected;
	private long replacementSeed;
	private int tournamentSizeReplacement;
	private String generationSelected;
	
	public ConfigFile()  {
		
		this.mutationProbability = 0.01;
		this.mutationSeed = 1;
		this.crossProbability = 0.05;
		this.crossSelected = ViewDefinitions.geneticProgrammingCrossType;
		this.crossSeed = 2;
		this.populationSize = 40;
		this.populationSeed = 3;
		this.reparationSeed = 4;
		this.stopConditionSelected = ViewDefinitions.numberOfGenerationsStopCondition;
		this.numberEvaluations = 50;
		this.generationsWithoutImprovement = 10;
		this.optimusValue = 0.0;
		this.numberGenerations = 10;
		this.minutesTimeLimit = 15;
		this.hoursTimeLimit = 0;
		this.selectionSelected = ViewDefinitions.tournamentSelection;
		this.tournamentSizeSelection = 3;
		this.selectionSeed = 5;
		this.evaluationSelected = ViewDefinitions.fileEvaluation; 
		this.replacementSelected = ViewDefinitions.worstReplacement;
		this.replacementSeed = 6;
		this.tournamentSizeReplacement = 3;
		this.generationSelected = ViewDefinitions.randomGeneration;
	}
	
	public void setMutationProbability(double mutationProb)  {
		this.mutationProbability = mutationProb;
	}
	
	public double getMutationProbability()  {
		return this.mutationProbability;
	}

	public double getCrossProbability() {
		return crossProbability;
	}

	public void setCrossProbability(double crossProbability) {
		this.crossProbability = crossProbability;
	}

	public int getPopulationSize() {
		return populationSize;
	}

	public void setPopulationSize(int populationSize) {
		this.populationSize = populationSize;
	}

	public long getReparationSeed() {
		return reparationSeed;
	}

	public void setReparationSeed(long reparationSeed) {
		this.reparationSeed = reparationSeed;
	}

	public String getStopConditionSelected() {
		return stopConditionSelected;
	}

	public void setStopConditionSelected(String conditionSelected) {
		this.stopConditionSelected = conditionSelected;
	}

	public String getCrossSelected() {
		return crossSelected;
	}

	public void setCrossSelected(String crossSelected) {
		this.crossSelected = crossSelected;
	}

	public String getSelectionSelected() {
		return selectionSelected;
	}

	public void setSelectionSelected(String selectionSelected) {
		this.selectionSelected = selectionSelected;
	}
	
	public long getMutationSeed() {
		return mutationSeed;
	}

	public void setMutationSeed(long mutationSeed) {
		this.mutationSeed = mutationSeed;
	}

	public String getEvaluationSelected() {
		return evaluationSelected;
	}

	public void setEvaluationSelected(String evaluationSelected) {
		this.evaluationSelected = evaluationSelected;
	}

	public String getReplacementSelected() {
		return replacementSelected;
	}

	public void setReplacementSelected(String replacementSelected) {
		this.replacementSelected = replacementSelected;
	}

	public String getGenerationSelected() {
		return generationSelected;
	}

	public void setGenerationSelected(String generationSelected) {
		this.generationSelected = generationSelected;
	}

	public long getCrossSeed() {
		return crossSeed;
	}

	public void setCrossSeed(long crossSeed) {
		this.crossSeed = crossSeed;
	}

	public int getTournamentSizeSelection() {
		return tournamentSizeSelection;
	}

	public void setTournamentSizeSelection(int tournamentSizeSelection) {
		this.tournamentSizeSelection = tournamentSizeSelection;
	}

	public long getSelectionSeed() {
		return selectionSeed;
	}

	public void setSelectionSeed(long selectionSeed) {
		this.selectionSeed = selectionSeed;
	}

	public int getNumberEvaluations() {
		return numberEvaluations;
	}

	public void setNumberEvaluations(int numberEvaluations) {
		this.numberEvaluations = numberEvaluations;
	}

	public int getGenerationsWithoutImprovement() {
		return generationsWithoutImprovement;
	}

	public void setGenerationsWithoutImprovement(int iterationsWithoutImprovement) {
		this.generationsWithoutImprovement = iterationsWithoutImprovement;
	}

	public double getOptimusValue() {
		return optimusValue;
	}

	public void setOptimusValue(double optimusValue) {
		this.optimusValue = optimusValue;
	}

	public int getNumberGenerations() {
		return numberGenerations;
	}

	public void setNumberGenerations(int numberGenerations) {
		this.numberGenerations = numberGenerations;
	}

	public long getPopulationSeed() {
		return populationSeed;
	}

	public void setPopulationSeed(long populationSeed) {
		this.populationSeed = populationSeed;
	}

	public int getMinutesTimeLimit() {
		return minutesTimeLimit;
	}

	public void setMinutesTimeLimit(int minutesTimeLimit) {
		this.minutesTimeLimit = minutesTimeLimit;
	}

	public int getHoursTimeLimit() {
		return hoursTimeLimit;
	}

	public void setHoursTimeLimit(int hoursTimeLimit) {
		this.hoursTimeLimit = hoursTimeLimit;
	}

	public long getReplacementSeed() {
		return replacementSeed;
	}

	public void setReplacementSeed(long replacementSeed) {
		this.replacementSeed = replacementSeed;
	}
	
	public void setTournamentSizeReplacement(int tournamentSizeReplacement)  {
		this.tournamentSizeReplacement = tournamentSizeReplacement;
	}
	
	public int getTournamentSizeReplacement()  {
		return this.tournamentSizeReplacement;
	}

	public void load() {

		Properties prop = new Properties();

		try {
			// load a properties file
			prop.load(new FileInputStream(filename));
			// mutacion
			this.mutationProbability = Double.valueOf(prop.getProperty(mutationProbabilityConfig));
			this.mutationSeed = Long.valueOf(prop.getProperty(mutationSeedConfig));
			// cruce
			this.crossProbability = Double.valueOf(prop.getProperty(crossProbabilityConfig));
			this.crossSelected = prop.getProperty(crossSelectedConfig);
			this.crossSeed = Long.valueOf(prop.getProperty(crossSeedConfig));
			// poblacion
			this.populationSize = Integer.valueOf(prop.getProperty(populationSizeConfig));
			// reparacion
			this.reparationSeed = Long.valueOf(prop.getProperty(reparationSeedConfig));
			// condicion de parada
			this.stopConditionSelected = prop.getProperty(stopConditionSelectedConfig);
			this.numberEvaluations = Integer.valueOf(prop.getProperty(numberEvaluationsStopConditionConfig));
			this.generationsWithoutImprovement = Integer.valueOf(prop.getProperty(generatiosWithoutImprovementStopConditionConfig));
			this.optimusValue = Double.valueOf(prop.getProperty(optimusValueStopConditionConfig));
			this.numberGenerations = Integer.valueOf(prop.getProperty(numberEvaluationsStopConditionConfig));
			this.minutesTimeLimit = Integer.valueOf(prop.getProperty(minutesTimeLimitStopConditionConfig));
			this.hoursTimeLimit = Integer.valueOf(prop.getProperty(hoursTimeLimitStopConditionConfig));
			// seleccion
			this.selectionSelected = prop.getProperty(selectionSelectedConfig);
			this.selectionSeed = Long.valueOf(prop.getProperty(selectionSeedConfig));
			this.tournamentSizeSelection = Integer.valueOf(prop.getProperty(tournamentSizeSelectionConfig));
			// evaluacion
			this.evaluationSelected = prop.getProperty(evaluationSelectedConfig);
			// reemplazo
			this.replacementSelected = prop.getProperty(replacementSelectedConfig);
			this.replacementSeed = Long.valueOf(prop.getProperty(replacementSeedConfig));
			this.tournamentSizeReplacement = Integer.valueOf(prop.getProperty(tournamentSizeReplacementConfig));
			// generacion
			this.generationSelected = prop.getProperty(generationSelectedConfig);
			this.populationSeed = Long.valueOf(prop.getProperty(populationSeedConfig));
		} 
		catch (IOException e) {
			System.out.println("File not found. Default values loaded.");
		}
	}

	public void save() {

		Properties prop = new Properties();

		try {
			// set the properties value
			// mutacion
			prop.setProperty(mutationProbabilityConfig, String.valueOf(mutationProbability));
			prop.setProperty(mutationSeedConfig, String.valueOf(mutationSeed));
			// cruce
			prop.setProperty(crossProbabilityConfig, String.valueOf(crossProbability));
			prop.setProperty(crossSelectedConfig, crossSelected);
			prop.setProperty(crossSeedConfig, String.valueOf(crossSeed));
			// poblacion
			prop.setProperty(populationSizeConfig, String.valueOf(populationSize));
			// reparacion
			prop.setProperty(reparationSeedConfig, String.valueOf(reparationSeed));
			// condicion de parada
			prop.setProperty(stopConditionSelectedConfig, stopConditionSelected);
			prop.setProperty(numberEvaluationsStopConditionConfig, String.valueOf(numberEvaluations));
			prop.setProperty(generatiosWithoutImprovementStopConditionConfig, String.valueOf(generationsWithoutImprovement));
			prop.setProperty(optimusValueStopConditionConfig, String.valueOf(optimusValue));
			prop.setProperty(numberEvaluationsStopConditionConfig, String.valueOf(numberGenerations));
			prop.setProperty(minutesTimeLimitStopConditionConfig, String.valueOf(minutesTimeLimit));
			prop.setProperty(hoursTimeLimitStopConditionConfig, String.valueOf(hoursTimeLimit));
			// seleccion
			prop.setProperty(selectionSelectedConfig, selectionSelected);
			prop.setProperty(selectionSeedConfig, String.valueOf(selectionSeed));
			prop.setProperty(tournamentSizeSelectionConfig, String.valueOf(tournamentSizeSelection));
			// evaluacion
			prop.setProperty(evaluationSelectedConfig, evaluationSelected);
			// reemplazo
			prop.setProperty(replacementSelectedConfig, replacementSelected);
			prop.setProperty(replacementSeedConfig, String.valueOf(replacementSeed));
			prop.setProperty(tournamentSizeReplacementConfig, String.valueOf(tournamentSizeReplacement));
			// generacion
			prop.setProperty(generationSelectedConfig, generationSelected);
			prop.setProperty(populationSeedConfig, String.valueOf(populationSeed));
			
			prop.store(new FileOutputStream(filename), null);
		} 
		catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
